package com.stack.knowyouweb.controller;

import com.google.gson.Gson;

import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoubean.bean.FileType;
import com.stack.knowyoubean.bean.RepJson;
import com.stack.knowyouservice.service.LabReportService;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 实验报告服务层
 *
 * @author stack
 */
@RestController
@Scope("singleton")
@RequestMapping("/api")
public class LabReportController {
    @Resource
    private LabReportService labReportService;

    @Resource
    private Gson gson;

    @GetMapping("/labreport")
    public String reportInfo(@RequestParam(value = "path", required = false, defaultValue = "") String filePath, HttpSession session, Integer page, Integer limit, Authentication authentication) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(null);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {

            Map<String, Object> resultMap = labReportService.reportInfo(sid, filePath, page, limit, authentication);
            RepJson<List<FileType>> repJson = new RepJson<>();
            repJson.setData((List<FileType>) resultMap.get("reportList"));
            repJson.setMsg("查询成功");
            repJson.setCount((Integer) resultMap.get("count"));
            return gson.toJson(repJson);
        }
    }

    @PostMapping("/labreport")
    public String reportUpload(@RequestParam(value = "file", required = false) MultipartFile file, HttpSession session, Authentication authentication) {
        Integer sid = (Integer) session.getAttribute("courseid");
        RepJson<String> repJson = new RepJson<>();
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
        } else {
            boolean b = labReportService.reportUpload(file, sid, authentication);
            if (b) {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setMsg("上传成功");
                repJson.setData(GlobalConst.SUCCESS_TYPE);
            } else {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setMsg("上传失败");
                repJson.setData(GlobalConst.ERROR_TYPE);
            }
        }
        return gson.toJson(repJson);
    }

    /**
     * RequestParam 对传递参数限制
     *
     * @param fileName 文件名
     * @param session  session
     * @return
     */
    @DeleteMapping("labreport")
    public String reportDelete(@RequestParam(required = true, defaultValue = "") String fileName, @RequestParam(required = true, defaultValue = "") String fileType, HttpSession session, Authentication authentication) {
        Integer sid = (Integer) session.getAttribute("courseid");
        RepJson<String> repJson = new RepJson<>();
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
        } else {
            boolean b = labReportService.reportDelete(fileName, authentication, fileType, sid);
            if (b) {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setMsg("删除成功");
                repJson.setData(GlobalConst.SUCCESS_TYPE);
            } else {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setMsg("删除失败");
                repJson.setData(GlobalConst.ERROR_TYPE);
            }
        }
        return gson.toJson(repJson);
    }

}
